package com.triangle.cashier;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.triangle.cashier.adapter.ProductCategoryAdapter;
import com.triangle.cashier.bean.ProductCategoryItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 选择商品列表loginEvent
 */
public class ProductListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ListView productCategoryListView;
    private ProductCategoryAdapter selectedCategoryAdapter;

    private List<ProductCategoryItem> list;

    private GridView productGridView;
    private EditText goodsNumEditText;

    private List<Map<String, Object>> dataList;
    private SimpleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);

        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }

        initView();
        initData();
    }

    private void initView() {
        productCategoryListView = (ListView) findViewById(R.id.product_category);
        productGridView = (GridView) findViewById(R.id.product_gridview);
        goodsNumEditText = (EditText) findViewById(R.id.goods_num_ET);
        productCategoryListView.setOnItemClickListener(this);
    }

    private void initData() {
        //初始化左边列表
        list = new ArrayList<>();
        list.add(new ProductCategoryItem("0", R.drawable.icon_other_category, "所有分类"));
        list.add(new ProductCategoryItem("1", R.drawable.icon_girl_clothes, "女装"));
        list.add(new ProductCategoryItem("2", R.drawable.icon_kitchen, "厨具"));
        list.add(new ProductCategoryItem("3", R.drawable.icon_fruits, "水果"));
        list.add(new ProductCategoryItem("4", R.drawable.icon_vegetables, "蔬菜"));
        list.add(new ProductCategoryItem("5", R.drawable.icon_girl_clothes, "泳装"));
        list.add(new ProductCategoryItem("6", R.drawable.icon_kitchen, "厨卫"));
        list.add(new ProductCategoryItem("7", R.drawable.icon_fruits, "瓜果"));
        list.add(new ProductCategoryItem("8", R.drawable.icon_vegetables, "海鲜"));
        list.add(new ProductCategoryItem("9", R.drawable.icon_other_category, "其他"));
        selectedCategoryAdapter = new ProductCategoryAdapter(getApplicationContext(), list, new ProductCategoryAdapter.OnChangeOnListener() {
            @Override
            public void refresh() {
            }
        });
        productCategoryListView.setAdapter(selectedCategoryAdapter);


        //初始化右边商品信息
        dataList = new ArrayList<Map<String, Object>>();
        initProductInfo(50);

        String[] from = {"product_icon", "product_name", "product_price", "product_stock"};
        int[] to = {R.id.product_icon, R.id.product_name, R.id.product_price, R.id.product_stock};
        adapter = new SimpleAdapter(this, dataList, R.layout.product_info_item, from, to);

        productGridView.setAdapter(adapter);

        productGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
//                AlertDialog.Builder builder = new AlertDialog.Builder(ProductListActivity.this);
//                builder.setTitle("提示").setMessage(dataList.get(arg2).get("product_name").toString()).create().show();
                finish();
            }
        });

    }

    private void initProductInfo(int count) {
        dataList.clear();
        for (int i = 0; i <= count; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("product_icon", R.drawable.product_icon);
            map.put("product_name", "商品" + i);
            map.put("product_price", "价格:" + (i + 10) + "元");
            map.put("product_stock", "库存:" + (i + 100));
            dataList.add(map);
        }
    }


    /**
     * 按货物编号查询
     *
     * @param v v
     */
    public void searchNumEvent(View v) {

    }

    public void backEvent(View v) {
        finish();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        initProductInfo(5+new Random().nextInt(10)+position);
        productGridView.invalidateViews();  //刷新
//        Toast.makeText(this, "listview的item被点击了！，点击的位置是-->" + list.get(position).getCategoryText(), Toast.LENGTH_SHORT).show();

    }
}
